package com.meizu.asum.richtextsdk.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.meizu.asum.richtextsdk.R;
import com.meizu.asum.richtextsdk.activity.designer.MainActivityDesigner;
import com.meizu.asum.richtextsdk.tools.selectpictools.selecter.XPictureSelector;
import com.meizu.asum.richtextsdk.tools.selectpictools.selecter.XPictureSelectorCallBack;
import com.meizu.asum.richtextsdk.tools.selectpictools.tools.PermissionTools;
import com.meizu.asum.richtextsdk.uiframework.xuidesign.utils.XDesigner;
import com.meizu.asum.richtextsdk.view.RichEditText;

import cn.dreamtobe.kpswitch.util.KPSwitchConflictUtil;
import cn.dreamtobe.kpswitch.util.KeyboardUtil;

public class MainActivity extends Activity {
    private XDesigner designer;
    private MainActivityDesigner uiDesigner;

    private XPictureSelector selector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        designer = new XDesigner();
        uiDesigner = (MainActivityDesigner) designer.design(this, R.layout.activity_main);

        selector = new XPictureSelector(this);
        new PermissionTools().check(this, null, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE);

        addListener();

        uiDesigner.editText.setRichText("<https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=8593594540.jpg>123{:emo_apple_011}");
    }

    private void addListener() {
        KeyboardUtil.attach(this, uiDesigner.panelLayout, new KeyboardUtil.OnKeyboardShowingListener() {
            @Override
            public void onKeyboardShowing(boolean isShowing) {
                if (!isShowing) {
                    uiDesigner.editText.setSelection(uiDesigner.editText.getSelectionStart());
                }
            }
        });
        KPSwitchConflictUtil.attach(uiDesigner.panelLayout, uiDesigner.emoticonButton, uiDesigner.editText);

        uiDesigner.editText.setCallBack(new RichEditText.RichEditTextCallBack() {
            @Override
            public void inputSymbol(String symbol) {
                gotoSelectSymbolActivity(symbol);
            }

            @Override
            public void tooLong(String theInsertString) {
                Log.i("XJW", "输入过长：" + theInsertString);
            }
        });

        for (ImageView emoticonImageView : uiDesigner.emoticonImageViews) {
            emoticonImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getTag().equals("")) {
                        uiDesigner.editText.deleteInput();
                    } else {
                        uiDesigner.editText.insertEmoticon((String) v.getTag());
                    }
                }
            });
        }

        uiDesigner.picButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selector.cachePath(null).proportion(1, 2).needCrop(false).fromPicture(new XPictureSelectorCallBack() {
                    public void complete(boolean isSuccess, Uri uri) {
                        if (isSuccess) {
                            uiDesigner.editText.insertPic(uri.getPath());
                        }
                    }
                });
            }
        });

        uiDesigner.topicButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uiDesigner.editText.insertTopic("#美团#", false);
            }
        });
    }

    private void gotoSelectSymbolActivity(String symbol) {
        Intent intent = new Intent(this, SelectSymbolActivity.class);
        intent.putExtra("symbol_type", symbol);
        this.startActivityForResult(intent, 1860);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (uiDesigner.panelLayout.getVisibility() == View.VISIBLE) {
                KPSwitchConflictUtil.hidePanelAndKeyboard(uiDesigner.panelLayout);
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        selector.disposeResult(requestCode, resultCode, data);

        if (requestCode == 1860 && resultCode == 10086) {
            String symbolType = data.getStringExtra("symbol_type");
            String symbolName = data.getStringExtra("symbol");
            uiDesigner.editText.insertTopic(symbolName, true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        uiDesigner.editText.clear();
    }
}
